<?php

namespace app\index\controller;

use app\index\model\DetailitemModel;
use think\Controller;
use app\index\controller\Basecontroller;

class Getdetailitem extends Basecontroller
{
  private $Model;

  public function __construct(){
      $this ->Model = new DetailitemModel();
      parent::__construct();
  }
  
  public function select(){//获取子商品详情
    $gid = getPost()['id'];
    $res = $this->Model->select($gid);//子订单信息
    $sponsorRes = $this->Model->goods($gid);//获取发起人id
    $sponsor = $sponsorRes['g_sponsor'];
    $sponsor_info = $this->Model->sponsor_user($sponsor);//发起人信息
    $numRes = $this->Model->num($sponsor);//发起数量
    if($sponsor_info){
      $sponInfo=$sponsor_info;
    }else{
      $sponInfo=0;
    }
    $num = count($numRes);
    //猜你喜欢，点赞最多
    $likeRes = $this->Model->goodsLikes();
    $allRes = [
      $res,
      $sponInfo,
      $num,
      $likeRes
    ];
    return res(1000,'获取成功',$allRes);
  }
  public function toOrder(){//生成订单
    $gid=getPost()['gid'];//商品id
    $g_item_id=getPost()['g_item_id'];//子商品id

    $token = getheaders()['Access-Token'];
    if (!$token) {
      return res('1001', 'token 无效');
    }
    $userRes = $this->redis->get($token);
    if (!$userRes) {
      return res('1001', 'token 无效');
    }
    $userRes = json_decode($userRes, true);
    $uid = $userRes['uid'];//用户id
    
    $state=1;//未付款
    // 获取订单id
    $time = time();
    $srand = rand(1000,9999);
    $order_id=(string)$time.(string)$srand;
    //生成订单
    $res = $this->Model->order($gid,$g_item_id,$uid,$state,$order_id);
    if($res){
      return res(1000,'生成订单成功',$order_id);
    }else{
      return res(1001,'生成订单失败');
    }
  }
  public function order(){//支付订单
    $gid=getPost()['gid'];//商品id
    $order_id = getPost()['order_id'];//订单编号，用于修改状态的条件
    $price = getPost()['money'];//价格

    $token = getheaders()['Access-Token'];
    if (!$token) {
      return res('1001', 'token 无效');
    }
    $userRes = $this->redis->get($token);
    if (!$userRes) {
      return res('1001', 'token 无效');
    }
    $userRes = json_decode($userRes, true);
    $uid = $userRes['uid'];//用户id

    //判断余额是否足够
    if((float)$price>$userRes['money']){
      return res(1001,'余额不足');
    }
    //减去余额
    $newMoney = $userRes['money']-(float)$price;
    $moneyRes = $this->Model->newMoney($uid,$newMoney);
    if($moneyRes){
      $state=2;//待收货
      $changeState = $this->Model->updateOrder($order_id,$state);
      if($changeState){
        $nowGoods = $this->Model->goods($gid);//当前信息
        $goodsMoney = $nowGoods['g_start_money']+(float)$price;//最新金额
        $nowSponsor = $nowGoods['buy_num']+1;//购买人数
        $newGoods = $this->Model->updateGoods($gid,$goodsMoney,$nowSponsor);
        if($newGoods){
          $newRes = $this->Model->goods($gid);
          return res(1000,'下单成功',$newRes);
        }else{
          return res(1001,'数据错误');
        }
      }else{
        return res(1001,"状态修改失败");
      }
    }else{
      return res(1001,'下单失败');
    }
  }
  public function wusi(){//无私支持
    $money = getPost()['money'];//支持金额
    $gid = getPost()['gid'];//支持的商品
    $token = getheaders()['Access-Token'];
    if (!$token) {
      return res('1001', 'token 无效');
    }
    $userRes = $this->redis->get($token);
    if (!$userRes) {
      return res('1001', 'token 无效');
    }
    $userRes = json_decode($userRes, true);
    if((float)$money>$userRes['money']){//余额判断
      return res(1001,'余额不足');
    }
    $newMoney = $userRes['money']-(float)$money;
    $moneyRes = $this->Model->newMoney($userRes['uid'],$newMoney);//修改用户金额
    if($moneyRes){
      //增加该商品目前金额
      $nowGoods = $this->Model->goods($gid);//当前信息
      $goodsMoney = $nowGoods['g_start_money']+(float)$money;//最新金额
      $nowSponsor = $nowGoods['buy_num']+1;//购买人数
      $newGoods = $this->Model->updateGoods($gid,$goodsMoney,$nowSponsor);
      if($newGoods){
        $newRes = $this->Model->goods($gid);
        return res(1000,'支持成功，感谢您的支持',$newRes);
      }else{
        return res(1001,'数据错误');
      }
    }else{
      return res(1001,'支持失败');
    }
  }
  //主要视频和tupian
  public function main(){
    $gid = getPost()['gid'];
    $nowGoods = $this->Model->goods($gid);//当前信息 
    if($nowGoods['g_video']=='无' || $nowGoods['g_video']==null){
      $video = 1;
    }else{
      $video = $nowGoods['g_video'];
    }
    $mainImgRes = $this->Model->mainImg($gid);
    $data = [
      $video,
      $mainImgRes
    ];
    if($mainImgRes){
      return res(1000,'main信息',$data);
    }else{
      return res(1001,'获取失败');
    }
  }
  //进程
  public function jinCheng(){
    $gid = getPost()['gid'];
    $res = $this->Model->jselect((int)$gid);
    $allRes = $this->Model->problem($gid);
    if($allRes){
      $Problem = $allRes;
    }else{
      $tData['time']=date('Y-m-d',time());
      $tData['title']='亲~该项目还没有提问哦';
      $Problem = [
        $tData
      ];
    }
    $newRes = [
      $res,
      $Problem,
    ];
    return res(1000,'获取成功',$newRes);
  }
  //提问
  public function tiWen(){
    $gid = getPost()['gid'];
    $text = getPost()['text'];
    $phone = getPost()['phone'];
    $token = getheaders()['Access-Token'];
    if (!$token) {
      return res('1001', 'token 无效');
    }
    $userRes = $this->redis->get($token);
    if (!$userRes) {
      return res('1001', 'token 无效');
    }
    $userRes = json_decode($userRes, true);
    $uid = $userRes['uid'];//用户id
    $time = date('Y-m-d',time());
    $res = $this->Model->tiWen($gid,$text,$phone,$uid,$time);
    if($res){
      return res(1000,'提问成功');
    }else{
      return res(1001,'提问失败');
    }
  }
  //删除订单
  public function deleteOrder(){
    $order_id = getPost()['id'];
    $res = $this->Model->deleteOrder($order_id);
    if($res){
      return res(1000,'删除成功');
    }else{
      return res(1001,'删除失败');
    }
  }
  //个人中心支付订单
  public function orderGet(){
    $gid=getPost()['gid'];//商品id
    $order_id = getPost()['order_id'];//订单编号，用于修改状态的条件
    $price = getPost()['money'];//价格

    $token = getheaders()['Access-Token'];
    if (!$token) {
      return res('1001', 'token 无效');
    }
    $userRes = $this->redis->get($token);
    if (!$userRes) {
      return res('1001', 'token 无效');
    }
    $userRes = json_decode($userRes, true);
    $uid = $userRes['uid'];//用户id

    //判断余额是否足够
    if((float)$price>$userRes['money']){
      return res(1001,'余额不足');
    }
    $newMoney = (float)$userRes['money']-(float)$price;
    $moneyRes = $this->Model->newMoney($uid,$newMoney);
    if($moneyRes){
      $state=2;//待收货
      $changeState = $this->Model->updateOrder($order_id,$state);
      if($changeState){
        return res(1000,'下单成功');
      }else{
        return res(1001,"状态修改失败");
      }
    }else{
      return res(1001,'下单失败');
    }
  }
  //收到商品订单
  public function toGet(){
    $order_id = getPost()['order_id'];
    $res = $this->Model->getOrder($order_id,3);
    if($res){
      return res(1000,"感谢您的支持，您可以评价该商品");
    }else{
      return res(1001,'数据错误');
    }
  }
  //评价商品
  public function pingfen(){
    $order_id = getPost()['order_id'];
    $res = $this->Model->getOrder($order_id,4);
    if($res){
      return res(1000,'感谢您的评价');
    }else{
      return res(1001,'数据错误');
    }
  }
}